package com.xms.queryview.service;

import com.xms.core.exception.XmsException;
import com.xms.core.module.ModuleCollection;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.model.DependentDescriptor;
import com.xms.dependency.service.IDependencyCreaterService;
import com.xms.dependency.service.IDependencyDeleterService;
import com.xms.dependency.service.IDependencyUpdaterService;
import com.xms.queryview.core.QueryViewDefaults;
import com.xms.queryview.entity.QueryView;
import com.xms.ribbonbutton.core.RibbonButtonDefaults;
import com.xms.schema.core.AttributeDefaults;
import com.xms.schema.entity.Attribute;
import com.xms.sdk.core.query.QueryExpression;
import com.xms.sdk.extensions.IQueryMetadataFinder;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import com.xms.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * QueryViewDependency
 * </p>
 *
 * @author migo
 * @since 2021/10/24
 */
@Service
public class QueryViewDependency implements IQueryViewDependency {
    private IQueryViewFinderService _queryViewFinderService;
    private IDependencyCreaterService _dependencyCreaterService;
    private IDependencyUpdaterService _dependencyUpdaterService;
    private IDependencyDeleterService _dependencyDeleterService;
    private IQueryMetadataFinder _queryMetadataFinder;

    @Autowired
    public QueryViewDependency(IQueryViewFinderService queryViewFinderService,
                               IQueryMetadataFinder queryMetadataFinder,
                               IDependencyCreaterService dependencyCreaterService,
                               IDependencyUpdaterService dependencyUpdaterService,
                               IDependencyDeleterService dependencyDeleterService) {
        _queryViewFinderService = queryViewFinderService;
        _dependencyCreaterService = dependencyCreaterService;
        _dependencyUpdaterService = dependencyUpdaterService;
        _dependencyDeleterService = dependencyDeleterService;
        _queryMetadataFinder = queryMetadataFinder;
    }

    @Override
    public boolean create(QueryView entity) throws XmsException, SQLException {
        //依赖于字段
        QueryExpression queryExp = JsonUtil.toBean(entity.getFetchconfig(), QueryExpression.class);
        List<Attribute> requiredAttributes = _queryMetadataFinder.getAttributes(queryExp);
        _dependencyCreaterService.create(QueryViewDefaults.MODULE_NAME, entity.getQueryviewid(),
                AttributeDefaults.MODULE_NAME, requiredAttributes.stream().map(x -> x.getAttributeid()).collect(Collectors.toList()));

        //依赖于按钮
        if (entity.getIscustombutton() && StringUtil.notEmpty(entity.getCustombuttons())) {
            List<UUID> buttonIds = JsonUtil.toList(entity.getCustombuttons(), UUID.class);
            if (CollectionUtil.notEmpty(buttonIds)) {
                _dependencyCreaterService.create(QueryViewDefaults.MODULE_NAME, entity.getQueryviewid(), RibbonButtonDefaults.MODULE_NAME, buttonIds);
            }
        }
        return true;
    }

    @Override
    public boolean update(QueryView entity) throws XmsException, SQLException {
        //依赖于字段
        QueryExpression queryExp = JsonUtil.toBean(entity.getFetchconfig(), QueryExpression.class);
        List<Attribute> requiredAttributes = _queryMetadataFinder.getAttributes(queryExp);
        _dependencyUpdaterService.update(QueryViewDefaults.MODULE_NAME, entity.getQueryviewid(), AttributeDefaults.MODULE_NAME
                , requiredAttributes.stream().map(x -> x.getAttributeid()).collect(Collectors.toList()));
        //依赖于按钮
        if (entity.getIscustombutton() && StringUtil.notEmpty(entity.getCustombuttons())) {
            List<UUID> buttonIds = JsonUtil.toList(entity.getCustombuttons(), UUID.class);
            if (CollectionUtil.notEmpty(buttonIds)) {
                _dependencyUpdaterService.update(QueryViewDefaults.MODULE_NAME, entity.getQueryviewid(), RibbonButtonDefaults.MODULE_NAME, buttonIds);
            }
        }
        return true;
    }

    @Override
    public boolean delete(UUID... id) throws SQLException, XmsDependencyException {
        return _dependencyDeleterService.deleteByDependentId(QueryViewDefaults.MODULE_NAME, id);
    }

    @Override
    public DependentDescriptor getDependent(UUID dependentId) {
        QueryView result = _queryViewFinderService.getById(dependentId);
        return result != null ? new DependentDescriptor() {
            {
                name = result.getName();
            }
        } : null;
    }

    @Override
    public int getComponentType() {
        return ModuleCollection.getIdentity(QueryViewDefaults.MODULE_NAME);
    }
}
